package com.zzq.my.jetpacks.net.entity

import android.util.Log
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.JsonParseException
import retrofit2.HttpException
import java.net.SocketTimeoutException
import java.util.concurrent.CancellationException
import kotlin.math.log

enum class HttpError(var code: Int, var errorMsg: String) {
    TOKEN_EXPIRE(401, "token is expired"),//token 失效
//    PARAMS_ERROR(4003, "params is error")
    // ...... more
}

internal fun handlingApiExceptions(code: Int?, errorMsg: String?) = when (code) {
    HttpError.TOKEN_EXPIRE.code -> ToastUtils.showShort(HttpError.TOKEN_EXPIRE.errorMsg)
//    HttpError.PARAMS_ERROR.code -> toast(HttpError.PARAMS_ERROR.errorMsg)
    else -> errorMsg?.let { ToastUtils.showShort(it); }
}

internal fun handlingExceptions(e: Throwable) = when (e) {
    is HttpException -> {
        if (e.code() == 401) {
            ToastUtils.showShort("401 token 失效；请重新登录！");
        } else if (e.code() == 403) {
            ToastUtils.showShort("403 签名失败");
        } else {
            ToastUtils.showShort(e.message);
        }
    }
//    is CancellationException -> {
//        //协程取消异常
//    }
    is SocketTimeoutException -> {
        //链接超时
        ToastUtils.showShort("网络链接超时,请稍后重试！");
    }
    is JsonParseException -> {
        //解析异常
        ToastUtils.showShort("数据解析异常,请稍后重试！");
    }
    else -> {
        //其它
        ToastUtils.showShort("网络异常,请稍后重试！");
    }
}